package com.xrkj.b2c.service.impl.bus;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xrkj.b2c.dao.bus.BusOrderMapper;
import com.xrkj.b2c.dao.sys.SysAdminMapper;
import com.xrkj.b2c.dao.sys.SysCustomerMapper;
import com.xrkj.b2c.model.bus.BusOrder;
import com.xrkj.b2c.model.sys.SysAdmin;
import com.xrkj.b2c.model.sys.SysCustomer;
import com.xrkj.b2c.service.bus.BusOrderService;
import com.xrkj.b2c.utils.DateUtils;
import com.xrkj.b2c.utils.UUIDUtil;

/**
 * 
 * <B>系统名称：</B><BR>
 * <B>模块名称：</B><BR>
 * <B>中文类名：</B>商品订单服务类<BR>
 * <B>概要说明：</B><BR>
 * 
 * @author 赵子靖
 * @since 2018年07月15日
 */
@Service
public class BusOrderServiceImpl implements BusOrderService {
    @Autowired
    private BusOrderMapper orderDao;
    @Autowired
    private SysCustomerMapper customerDao;
    @Autowired
    private SysAdminMapper sysAdminMapper;

    private Logger logger = Logger.getLogger(BusOrderServiceImpl.class);

    @Override
    public int deleteOrder(String id) {
        return orderDao.deleteByPrimaryKey(id);
    }

    @Override
    public int insertOrder(BusOrder order) {
        return orderDao.insertSelective(order);
    }

    @Override
    public int updateOrder(BusOrder order) {
        return orderDao.updateByPrimaryKeySelective(order);
    }

    @Override
    public BusOrder selectByPrimaryKey(String id) {
        return orderDao.selectByPrimaryKey(id);
    }


    /**
     * 
     * <B>方法名称：</B>查询订单列表<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2018年7月8日
     * @param paramter
     * @return
     */
    public List<BusOrder> findOrderList(Map<String, Object> paramter) {
        return orderDao.findOrderList(paramter);
    }

    /**
     * 通过customerId 添加客户名
     *
     * @param paramter
     * @return
     */
    public List<BusOrder> findOrderListAndCustomer(Map<String, Object> paramter) {
        List<BusOrder> orderList = orderDao.findOrderList(paramter);
        if (orderList == null || orderList.size() == 0) {
            return null;
        }
        for (BusOrder busOrder : orderList) {
            // 处理 客户
            SysCustomer sysCustomer = customerDao.selectByPrimaryKey(busOrder.getoCustomerId());
            if (sysCustomer != null) {
                busOrder.setoCustomerId(busOrder.getoCustomerId() + "," + sysCustomer.getcName());
            }
            // 处理 填表人
            SysAdmin sysAdmin = sysAdminMapper.selectByPrimaryKey(busOrder.getoSellerId());
            if (sysAdmin != null) {
                busOrder.setoSellerId(busOrder.getoSellerId() + "," + sysAdmin.getaNicheng());
            }
        }
        return orderList;
    }


    @Override
    public Integer findOrderTotal(Map<String, Object> paramter) {
        return orderDao.findOrderTotal(paramter);
    }

    /**
     * 
     * <B>方法名称：</B>批量生成订单<BR>
     * <B>概要说明：</B><BR>
     * 
     * @see com.xrkj.b2c.service.bus.BusOrderService#bathCreateOrderForPnr(java.lang.String)
     */
    @Override
    public boolean bathCreateOrderForPnr(String data,String loginUserId) {
        try {
            // 解析data
            JSONObject jsonData = JSONObject.parseObject(data);
            JSONArray orderArray = jsonData.getJSONArray("data");
            if (orderArray != null && orderArray.size() > 0) {
                for (int i = 0; i < orderArray.size(); i++) {
                    JSONObject orderJson = orderArray.getJSONObject(i);
                    if (orderJson != null) {
                        BusOrder order = new BusOrder();
                        order.setoId(UUIDUtil.id());
                        order.setoNo(this.createOrderNo());
                        order.setoPnr(orderJson.getString("pnr"));
                        order.setoName(orderJson.getString("name"));
                        order.setoRoute(orderJson.getString("route"));
                        order.setoTicketNo(orderJson.getString("ticketNo"));
                        order.setoCustomerId(orderJson.getString("customerId"));
                        order.setoTicketSource(orderJson.getString("ticketSource"));
                        order.setoSupplier(orderJson.getString("supplier"));
                        order.setoTicketPrice(new BigDecimal(orderJson.getString("ticketPrice")));
                        order.setoTicketTax(new BigDecimal(orderJson.getString("ticketTax")));
                        order.setoTicketSellPrice(new BigDecimal(orderJson.getString("ticketSellPrice")));
                        order.setoTicketSettlementPrice(new BigDecimal(orderJson.getString("ticketSettlementPrice")));
                        order.setoTicketRebate(new BigDecimal(orderJson.getString("ticketRebate")));
                        order.setoTicketProfit(new BigDecimal(orderJson.getString("ticketProfit")));
                        order.setoIssueDate(orderJson.get("issueDate")!=null?orderJson.getDate("issueDate"):null);
                        order.setoRemark(orderJson.getString("remark"));
                        order.setoSellerId(loginUserId);
                        order.setoStatus(1); //默认出票
                        
                        order.setoCreateTime(new Timestamp(System.currentTimeMillis()));
                        orderDao.insert(order);
                    }
                }
            }
            return true;
        } catch (Exception e) {
            logger.error("批量生成订单异常。" + e.getMessage());
            return false;
        }
    }

    /**
     * 
     * <B>方法名称：</B>订单编号生成<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2018年7月14日
     * @return
     */
    private String createOrderNo() {
        StringBuffer orderNo = new StringBuffer();
        // 日期+6位随机数 共20位数字组成订单编号
        orderNo.append(DateUtils.genYMDHMS()).append((int) ((Math.random() * 9 + 1) * 100000));
        return orderNo.toString();
    }
}
